package com.tencent.crazypenguin.dao;

import org.hibernate.Criteria;
import org.hibernate.QueryException;
import org.hibernate.Session;

import com.tencent.crazypenguin.config.HbmUtil;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class BaseDao<T> {
	

	@SuppressWarnings("unchecked")
	public T findById(Long id,T c) {
		
		Session s = HbmUtil.getSession();

		try{
			
			T t = (T) s.get(c.getClass(), id);
	
			return t;

		}finally{

			if(s!=null){
	
				s.close();
	
			}

		}

	}

		//删除用户

	public void remove(T entity) {
		
		Session s = HbmUtil.getSession();

		try{

			s.beginTransaction();
	
			s.delete(entity);
	
			s.getTransaction().commit();

		}finally{

			if(s!=null){
	
				s.close();
	
			}

		}

	}

		//添加一个用户

	public void save(T entity) {
		
		Session s = HbmUtil.getSession();

		try{

			s.beginTransaction();
	
			s.save(entity);
	
			s.getTransaction().commit();

		}finally{

			if(s!=null){
	
				s.close();
	
			}

		}

	}

		//更新一个用户

	public void update(T entity) {

		Session s = HbmUtil.getSession();

		try{

			s.beginTransaction();
	
			s.update(entity);
	
			s.getTransaction().commit();

		}finally{

			if(s!=null){
	
			s.close();
	
			}

		}
	}


	/**
	 *
	 * <pre>
	 * 查询所有记录,附带参数并排序,参数可模糊匹配,不分页
	 * </pre>
	 *
	 * @param cls
	 *            实体类名
	 * @param mapString
	 *            字符串参数列表
	 * @param mapByte
	 *            字字节型参数列表
	 * @param mapInt
	 *            整形参数列表
	 * @param mapLong
	 *            Long型参数列表
	 * @param sort
	 *            按某字段排序
	 * @param order
	 *            升/降序 asc/desc
	 * @return
	 */
	public List<T> getAll(Class<T> cls,
						  final Map<String, String> mapString,
						  final Map<String, Byte> mapByte, final Map<String, Integer> mapInt,
						  final Map<String, Long> mapLong, String sort, String order) throws QueryException {
		try {
			Criteria c = HbmUtil.getSession().createCriteria(cls);
			if (mapString != null && mapString.keySet() != null) {
				Iterator it = mapString.keySet().iterator();
				while (it.hasNext()) {
					String property = (String) it.next();
					String value = (String) mapString.get(property);
					if (value != null && !"".equals(value.trim())) {
						value = "%" + value + "%";
						c.add(Restrictions.like(property, value));
					}
				}
			}
			if (mapByte != null && mapByte.keySet() != null) {
				Iterator it = mapByte.keySet().iterator();
				while (it.hasNext()) {
					String property = (String) it.next();
					Byte value = (Byte) mapByte.get(property);
					if (value != null) {
						c.add(Restrictions.eq(property, value));
					}
				}
			}
			if (mapInt != null && mapInt.keySet() != null) {
				Iterator it = mapInt.keySet().iterator();
				while (it.hasNext()) {
					String property = (String) it.next();
					Integer value = (Integer) mapInt.get(property);
					if (value != null) {
						c.add(Restrictions.eq(property, value));
					}
				}
			}
			if (mapLong != null && mapLong.keySet() != null) {
				Iterator it = mapLong.keySet().iterator();
				while (it.hasNext()) {
					String property = (String) it.next();
					Long value = (Long) mapLong.get(property);
					if (value != null) {
						c.add(Restrictions.eq(property, value));
					}
				}
			}
			if (sort != null && order != null) {
				if ("asc".equals(order)) {
					c.addOrder(Order.asc(sort));
				} else {
					c.addOrder(Order.desc(sort));
				}
			} else {
				// 默认按照ID升序排序
				c.addOrder(Order.asc("id"));
			}
			List<T> list = c.list();
			return list;

		} catch (QueryException ex) {
			throw ex;
		}
	}


}
